package zhang.lao.service.console.base;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.transaction.annotation.Transactional;
import zhang.lao.build.mybatis.jdbc.auto.model.*;
import zhang.lao.build.tool.MD5;
import org.springframework.web.bind.annotation.RequestMapping;
import zhang.lao.build.tool.UUIDTool;
import zhang.lao.dao.base.SysUserDao;
import zhang.lao.build.tool.LzStringUtils;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import zhang.lao.build.mybatis.jdbc.auto.tool.ControllerQueryTool;
import zhang.lao.dao.base.SysUserRoleDao;
import zhang.lao.dao.base.WnFarmDao;
import zhang.lao.dao.base.WnUserFarmDao;
import zhang.lao.pojo.console.ConsoleContext;
import zhang.lao.pojo.console.req.BootStrapGridReq;
import zhang.lao.pojo.console.resp.BootStrapGridResp;

import zhang.lao.pojo.console.resp.HttpResult;
import zhang.lao.pojo.console.resp.HttpResultEnum;
import zhang.lao.pojo.console.resp.HttpResultUtil;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * title:
 * </p>
 * <p>
 * description:
 * </p>
 *
 * @create 张钦
 * @update
 * @version 1.0
 * @date
 */
@Service
public class SysUserService{
	@Resource
	private SysUserDao sysUserDao;
	@Resource
	private WnUserFarmDao wnUserFarmDao;
	@Resource
	private WnFarmDao wnFarmDao;
		public String add(){
			return "console/sysUser/sysUser_form";
		}


	public String authFarmPage(String userId,ModelMap modelMap,ConsoleContext consoleContext){
		SysUser sysUser=sysUserDao.selectByPrimaryKey(userId);
		modelMap.put("jds", getSelectJdHtmlByUserId(userId,consoleContext));
		modelMap.put("sys_user", sysUser);
		return "console/sysUser/user_farm";
	}

	@Transactional
	public HttpResult authFarm(String userId,String farmIds){
			WnUserFarmExample sysUserJdExample = new WnUserFarmExample();
			sysUserJdExample.createCriteria().andUserIdEqualTo(userId);
			wnUserFarmDao.deleteByExample(sysUserJdExample);
			String []jds= farmIds.split(",");
			if(jds!=null&&jds.length>0){
				for (String farmId : jds) {
					WnUserFarm sysUserJd = new WnUserFarm();
					sysUserJd.setId(UUIDTool.getUUID());
					sysUserJd.setFarmId(farmId);
					sysUserJd.setUserId(userId);
					wnUserFarmDao.insert(sysUserJd);
				}
			}
		return HttpResultUtil.buildSuccess();
	}

	public String getSelectJdHtmlByUserId(String userId, ConsoleContext consoleContext) {
		StringBuffer div=new StringBuffer();
		WnFarmExample wnFarmExample = new WnFarmExample();
		wnFarmExample.createCriteria().andCompanyIdEqualTo(consoleContext.getCompanyId());
		List<WnFarm> wnFarms = wnFarmDao.selectByExample(wnFarmExample);
		for (WnFarm wnFarm: wnFarms) {
			String checked="";
			if(containFarm(wnFarm.getId(), userId)){
				checked="checked=\"checked\"";
			}
			div.append("<div class=\"checkbox\"> \r\n");
			div.append("<label> \r\n");
			div.append("<input name=\"jd_name\" type=\"checkbox\" "+checked+" id=\""+wnFarm.getId()+"\" value=\""+wnFarm.getId()+"\" > \r\n");
			div.append("</label> \r\n");
			div.append(""+wnFarm.getName()+"\r\n");
			div.append("</div>  \r\n");
		}
		return div.toString();


	}
	public boolean containFarm(String farmId,String userId){
		WnUserFarmExample userFarmExample = new WnUserFarmExample();
		userFarmExample.createCriteria().andUserIdEqualTo(userId).andFarmIdEqualTo(farmId);
		return wnUserFarmDao.countByExample(userFarmExample)>0;
	}
		public String edit(ModelMap modelMap,String id){
			modelMap.put("sysUser", sysUserDao.selectByPrimaryKey(id));
			return "console/sysUser/sysUser_form";
		}
		public String select(){
			return "console/sysUser/sysUserSelect_table";
		}
		public String list(){
			return "console/sysUser/sysUser_table";
		}

		public
		BootStrapGridResp json(BootStrapGridReq bootGridReq, ConsoleContext consoleContext){
			Page page = PageHelper.offsetPage(bootGridReq.getOffset(), bootGridReq.getLimit());
			if(bootGridReq.getSort()!=null) {
				page.setOrderBy(LzStringUtils.chageStringUpCaseAnd_(bootGridReq.getSort()) + " " + bootGridReq.getOrder());
			}
			SysUserExample sysUserExample = new SysUserExample();
			SysUserExample.Criteria criteria = ControllerQueryTool.setSysUserCriteria(bootGridReq.getQuery(), sysUserExample.createCriteria());
			if(consoleContext.getLoginUserModel().getUser_type()==2){
				criteria.andCompanyIdEqualTo(consoleContext.getCompanyId());
			}
			List<SysUser> sysUserList = sysUserDao.selectByExample(sysUserExample);
			return new BootStrapGridResp(page.getTotal(),sysUserList);
		}

		@RequestMapping("/console/sys_user/save")
		public
		HttpResult save(String formObjectJson,ConsoleContext consoleContext){
				SysUser sysUser= JSON.parseObject(formObjectJson,SysUser.class);
				String id=sysUser.getSuId();
				if (id!=null) {
					sysUserDao.updateByPrimaryKeySelective(sysUser);
					return HttpResultUtil.buildSuccess();
				}else{
					sysUser.setCompanyId(consoleContext.getCompanyId());
					sysUser.setUserType((short) 2);
					sysUser.setUserPassword(MD5.MD5Encode("123456"));
					sysUser.setSuId(UUIDTool.getUUID());
					sysUserDao.insertSelective(sysUser);
					return HttpResultUtil.buildSuccess();
				}
		}

		@Transactional
		public HttpResult delete(String ids){
			String[]idsa=ids.split(",");
			for (String id : idsa) {
				if(id.equals("1")){
					continue;
				}
				SysUser sysUser = sysUserDao.selectByPrimaryKey(id);
				sysUser.setStatus((short) 0);
				sysUserDao.updateByPrimaryKey(sysUser);
			}
			return HttpResultUtil.buildSuccess();
		}

		public String base(){
			return "console/sysUser/sys_user_base";
		}

		public String changepass(){
			return "console/sysUser/sys_user_change_pass";
		}

		public
		HttpResult dochangepass(String old_password, String new_password, ConsoleContext consoleContext){
			SysUserExample sysUserExample=new SysUserExample();
			sysUserExample.createCriteria().andSuIdEqualTo(consoleContext.getUserId()).andUserPasswordEqualTo(MD5.MD5Encode(old_password));
			if(sysUserDao.countByExample(sysUserExample)>0){
				SysUser sysUser=sysUserDao.selectByPrimaryKey(consoleContext.getUserId());
				sysUser.setUserPassword(MD5.MD5Encode(new_password));
				sysUserDao.updateByPrimaryKeySelective(sysUser);
				return HttpResultUtil.buildSuccess();
			}else{
				return HttpResultUtil.buildError(HttpResultEnum.OLDPASSWORDERROR);
			}
		}
	}

